package guava;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 函数式编程
 *
 * @author Daniel
 */
public class FunctionsTest {

    public static void main(String[] args) {

        List<String> names = Lists.newArrayList("tom", "jack", "123456789");

        Function<String, String> substringFunction = new Function<String, String>() {

            @Nullable
            @Override
            public String apply(@Nullable String input) {
                return Objects.requireNonNull(input).length() <= 5 ? input: input.substring(0, 5);
            }
        };

        Function<String, String> toUpperCasefunction = new Function<String, String>() {
            @Nullable
            @Override
            public String apply(@Nullable String input) {
                return Objects.requireNonNull(input).toUpperCase();
            }
        };

        Function<String, String> composeFunction = Functions.compose(substringFunction, toUpperCasefunction);

        Collection<String> result = Collections2.transform(names, composeFunction);

        result.forEach(System.out::println);
    }

}
